package com.ccl.iot.dialogs;

import android.content.Context;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.ccl.iot.R;
import com.ccl.iot.managers.UserManager;
import com.ccl.iot.publics.Messager;


public class ChangePasswordDialog extends CustomDialog {
	private UserManager iUserManager;
	
//	private TextView iAccount = null;
	private EditText iOldPwd = null;
	private EditText iNewPwd = null;
	private EditText iNewPwdConfirm = null;
    private View mView;
    private Button mBtnOk;
    private Button mBtnCancel;

    public ChangePasswordDialog(Context context) {
		super(context, R.style.DialogTheme);
		
		iUserManager = UserManager.GetDefaultManager();
        initialize();
	}

	@Override
	public View getView() {
        mView = View.inflate(getContext(), R.layout.dialog_change_password, null);
		return mView;
	}

	private void initialize(){

//		iAccount = (TextView) mView.findViewById(R.id.iAccount);
//		if (iAccount != null && iUserManager != null){
//			iAccount.setText(iUserManager.getLogonUserName());
//		}
		
		iOldPwd = (EditText) mView.findViewById(R.id.iOldPassword);
		iNewPwd = (EditText) mView.findViewById(R.id.iNewPassword);
		iNewPwdConfirm = (EditText) mView.findViewById(R.id.iNewPasswordConfirm);

        mBtnOk = (Button) mView.findViewById(R.id.btn_ok);
        mBtnCancel = (Button) mView.findViewById(R.id.btn_cancel);

        mBtnOk.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onDone();
            }
        });

        mBtnCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                cancel();
            }
        });
	}

	protected boolean onDone() {
		if (iUserManager != null && iOldPwd != null && iNewPwd != null && iNewPwdConfirm != null){
			String tOldPwd = iOldPwd.getText().toString();
			String tNewPwd = iNewPwd.getText().toString();
			String tNewPwdConfirm = iNewPwdConfirm.getText().toString();
			
			if (tOldPwd.length() > 0 && tNewPwd.length() > 0 && tNewPwdConfirm.length() > 0){
				if (tNewPwd.equals(tNewPwdConfirm)){
					if (iUserManager.changePassword(tOldPwd, tNewPwd)){
						Messager.Show(R.string.ChangePasswordOK);
						
						return true;
					}
				}else{
					Messager.Show(R.string.Error_PasswordConfirmFaild);
				}
			}else{
				Messager.Show(R.string.Error_ParameterIncomplete);
			}
		}
		
		return false;
	}

}
